﻿using ArtMath.Core.Data;
using ArtMath.Helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.Line
{
    /// <summary>
    /// 点斜式直线（占满画布）
    /// </summary>
    [Serializable]
    public abstract class PointSlopeLineBase : StraightLineBase
    {
        List<KeyValuePair<PointD, double>> track = new List<KeyValuePair<PointD, double>>();
        public PointSlopeLineBase(SketchPad pad, string name, ToolTypeName toolType, bool addToList = true) : base(pad, name, toolType, addToList)
        {
        }
        public override double Slope
        {
            get
            {
                return base.Slope;
            }

            set
            {
                base.Slope = value;
                if (ToolType != ToolTypeName.ldzx)//两点直线用两点计算PSlope
                {
                    PSlope = Pad.kToPk(Slope);
                }
            }
        }
        //public override double PSlope
        //{
        //    get
        //    {
        //        return base.PSlope;
        //    }

        //    set
        //    {
        //        base.PSlope = value;
        //        pkAngle = double.IsNaN(PSlope) ? 0 : Math.Atan(PSlope) * Core.Helper.Angle.RToD;
        //        Pad.Canvas.Invalidate();
        //    }
        //}
        public override void AddTracks()
        {
            track.Add(new KeyValuePair<PointD, double>(pske.Location, PSlope));
        }
        public override void ClearTracks()
        {
            track.Clear();
            base.ClearTracks();
        }
        //public override PointD[] GetCrossPoints(ShapeBase shape, bool getNew = true)
        //{
        //    List<PointD> res = new List<PointD>();
        //    PointD[] pts = null;
        //    PointSlopeLineBase slb = shape as PointSlopeLineBase;
        //    if (slb != null)//直线+直线
        //    {
        //        res.Add(getNew ? Utility.GetCrossPoint(NewPa, NewPb, -NewPc, slb.NewPa, slb.NewPb, -slb.NewPc) :
        //            Utility.GetCrossPoint(Pa, Pb, -Pc, slb.Pa, slb.Pb, -slb.Pc));
        //    }
        //    else
        //    {
        //        ThreePointsPartialCircleBase tppcb = shape as ThreePointsPartialCircleBase;
        //        if (tppcb != null)
        //        {
        //            pts = getNew ? CurveIntersection.StraightLineArcIntersection(NewA, NewB, NewC, tppcb.NewCenter, tppcb.NewRadius, tppcb.NewStart, tppcb.NewSweep) :
        //                CurveIntersection.StraightLineArcIntersection(A, B, C, tppcb.Center, tppcb.Radius, tppcb.Start.RadValue, tppcb.Sweep.RadValue);
        //            if (Pad.Coordinate != null && pts != null)
        //            {
        //                for (int i = 0; i < pts.Length; i++)
        //                {
        //                    pts[i] = Pad.Coordinate.XYToPxy(pts[i]);
        //                }
        //            }
        //        }
        //        else
        //        {
        //            CircleLikeBase clb = shape as CircleLikeBase;
        //            if (clb != null)//直线+圆
        //            {
        //                pts = getNew ? CurveIntersection.StraightLineCircleIntersection(NewA, NewB, NewC, clb.NewCenter, clb.NewRadius) :
        //                     CurveIntersection.StraightLineCircleIntersection(A, B, C, clb.Center, clb.Radius);
        //                if (Pad.Coordinate != null && pts != null)
        //                {
        //                    for (int i = 0; i < pts.Length; i++)
        //                    {
        //                        pts[i] = Pad.Coordinate.XYToPxy(pts[i]);
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                EllipseBase eb = shape as EllipseBase;
        //                if (eb != null)//直线+椭圆
        //                {
        //                    pts = getNew ? CurveIntersection.StraightLineEllipseIntersection(NewA, NewB, NewC, eb.NewCenter, eb.NewLongAxisHalfLength, eb.NewShortAxisHalfLength, eb.NewFocusAngle) :
        //                         CurveIntersection.StraightLineEllipseIntersection(A, B, C, eb.Center, eb.LongAxisHalfLength, eb.ShortAxisHalfLength, eb.FocusAngle);
        //                    if (Pad.Coordinate != null && pts != null)
        //                    {
        //                        for (int i = 0; i < pts.Length; i++)
        //                        {
        //                            pts[i] = Pad.Coordinate.XYToPxy(pts[i]);
        //                        }
        //                    }
        //                }
        //                else
        //                {
        //                    HyperbolaBase hb = shape as HyperbolaBase;
        //                    if (hb != null)//直线+双曲线
        //                    {
        //                        pts = getNew ? CurveIntersection.StraightLineHyperbolaIntersection(NewA, NewB, NewC, hb.NewCenter, hb.NewRealAxisHalfLength, hb.NewImaginaryAxisHalfLength, hb.NewFocusAngle) :
        //                             CurveIntersection.StraightLineHyperbolaIntersection(A, B, C, hb.Center, hb.RealAxisHalfLength, hb.ImaginaryAxisHalfLength, hb.FocusAngle);
        //                        if (Pad.Coordinate != null && pts != null)
        //                        {
        //                            for (int i = 0; i < pts.Length; i++)
        //                            {
        //                                pts[i] = Pad.Coordinate.XYToPxy(pts[i]);
        //                            }
        //                        }
        //                    }
        //                    else
        //                    {
        //                        ParabolaBase pb = shape as ParabolaBase;
        //                        if (pb != null)//直线+抛物线
        //                        {
        //                            pts = getNew ? CurveIntersection.StraightLineParabolaIntersection(NewA, NewB, NewC, pb.NewCenter, pb.NewFocusDirectrixDistance, pb.NewFocusAngle) :
        //                                 CurveIntersection.StraightLineParabolaIntersection(A, B, C, pb.Center, pb.FocusDirectrixDistance, pb.FocusAngle);
        //                            if (Pad.Coordinate != null && pts != null)
        //                            {
        //                                for (int i = 0; i < pts.Length; i++)
        //                                {
        //                                    pts[i] = Pad.Coordinate.XYToPxy(pts[i]);
        //                                }
        //                            }
        //                        }
        //                        else
        //                        {
        //                            ICurveLike curs = shape as ICurveLike;
        //                            if (curs != null)//直线+曲线
        //                            {
        //                                foreach (PaintableCurve item in getNew ? curs.NewCurves : curs.Curves)
        //                                {
        //                                    pts = item.StraightLineCurveIntersection(this, (List<PointD> set) =>
        //                                    {
        //                                        if (shape is ICircle)
        //                                        {
        //                                            if (set.Count >= 2) return true;
        //                                        }
        //                                        return false;
        //                                    });
        //                                    if (pts != null) res.AddRange(pts);
        //                                }
        //                                pts = null;
        //                            }
        //                            else
        //                            {
        //                                if (shape is PolygonBase || shape is AngleNDivider || shape is CoordinateBase)//直线+直线组
        //                                {
        //                                    foreach (StraightLineBase item in shape.GetStraightLines())
        //                                    {
        //                                        pts = GetCrossPoints(item, getNew);
        //                                        if (pts != null) res.AddRange(pts);
        //                                    }
        //                                    pts = null;
        //                                }
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //    }
        //    if (pts != null) res.AddRange(pts);
        //    if (res.Count == 0) return null;
        //    return res.ToArray();
        //}
        public override void CheckInRegion(RectangleD rect)
        {
            SetInPicker(rect.IntersectStraightLine(pske.Location, PSlope));
            base.CheckInRegion(rect);
        }
        public override void PaintTrack(Graphics gra)
        {
            if (ShowTrack_)
            {
                //PointD len;
                //SizeD size = Pad.PSize;
                //Pen p = TrackPen;
                for (int i = 0; i < track.Count; i++)
                {
                    //gra.TranslateTransform((float)item.Key.X, (float)item.Key.Y);
                    //gra.RotateTransform((float)item.Value);
                    //len = Utility.CalcLength(PointD.FromPoint(Pad.PxyToMouseLocation(item.Key)) / Pad.Zoom.ToPointD(),
                    //    size, Angle.DegreeRound - item.Value);
                    //gra.DrawLine(p, (float)-len.X, 0, (float)len.Y, 0);
                    //gra.RotateTransform((float)-item.Value);
                    //gra.TranslateTransform((float)-item.Key.X, (float)-item.Key.Y);
                    gra.DrawStraightLine(Pad, TrackPen, track[i].Key, track[i].Value);
                }
            }
            base.PaintTrack(gra);
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible && IsCreationCompleted)
            {
                gra.DrawStraightLine(Pad, LinePen, pske.Location, PSlope);
            }
            base.Paint(gra);
        }
        public override void PaintVirtualObjects(Graphics gra)
        {
            if (CanDrawVirtual)
            {
                gra.DrawStraightLine(Pad, pske.Location, PSlope);
            }
            base.PaintVirtualObjects(gra);
        }
    }
}
